<?php
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    /**
    * Description of annonce
    *
    * @author Gidaff_01
    */
    class annonce extends Animus_FrontEnd_Controller {

        //put your code here
        function Annonce() {
            parent::Animus_FrontEnd_Controller();
            $this->load->model('Annonce_m');
            $this->load->helper('date');
        }

        function add_new_annonce() {
          if($this->session->userdata('user_id')!=""){
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'title',
                'Label' => 'Title',
                'rules' => 'required|xss_clean'
                ),
                array(
                'field' => 'description',
                'Label' => 'Description',
                'rules' => 'trim|required'
                ),
                array(
                'field' => 'content',
                'Label' => 'Content',
                'rules' => 'required'
                ),
                array(
                'field' => 'level',
                'Label' => 'Level',
                'rules' => 'required'
                )
                );
                $this->form_validation->set_rules($formVal);
                if ($this->form_validation->run() == TRUE) {
                    $image_path = './assets/annonce';
                    $thumb_path = $image_path . '/thumbs';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' => TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);
                    $level = $this->input->post('level');
                    $payamount = $this->db->get_where('animus_anounce_level',array('id'=>$level))->row()->prix;
                    $user_id = $this->session->userdata('user_id');
                    $user_email= $this->db->get_where('lehu_users',array('id'=>$user_id))->row()->email;
                    if ($this->upload->do_upload()) {
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        //delete_pro_file($upload_info['full_path']);


                        $query = $this->Annonce_m->get_day_allow_by_level($level);
                        if ($query->num_rows() > 0) {
                            $d = $query->row();
                        }
                        $dc = new DateClass();
                        $dc->Add("days", (int) $d->day_allow);
                        $string_date = $dc->ToString();
                        $expried_date = human_to_unix($string_date);

                        $data = array('title' => $this->input->post('title'),
                        'description' => $this->input->post('description'),
                        'content' => $this->input->post('content'),
                        'date_posted' => now(),
                        'date_expried' => $expried_date,
                        'posted_by' => $this->session->userdata('user_id'),
                        'level' => $level,
                        'active' => $level==1?1:0,
                        'images' => $upload_info['file_name'],
                        'sub_region' => $this->input->post('sub_region')
                        );
                    } else {

                        $query = $this->Annonce_m->get_day_allow_by_level($level);
                        if ($query->num_rows() > 0) {
                            $d = $query->row();
                        }
                        $dc = new DateClass();
                        $dc->Add("days", (int) $d->day_allow);
                        $string_date = $dc->ToString();
                        $expried_date = human_to_unix($string_date);
                        $data = array('title' => $this->input->post('title'),
                        'description' => $this->input->post('description'),
                        'content' => $this->input->post('content'),
                        'date_posted' => now(),
                        'date_expried' => $expried_date,
                        'posted_by' => $this->session->userdata('user_id'),
                        'sub_region' => $this->input->post('sub_region'),
                        'level' => $level,
                        'active' => $level==1?1:0
                        );
                    }
                    //print_r($data);die;
                    $this->Annonce_m->ajouter_annonce($data);
                    //echo $level;die;
                    if($level==1){
                        $this->_data['announce_id'] = $this->db->select('id')->from('animus_annonce')->order_by('id','DESC')->get()->row()->id;
                        $this->display_view('home/annonce/upgrade_unnonce');
                        /*Add information to database and prompt user to upgrade the announce level
                        * if yes, send to Bluepay
                        * if no, just ignore and return to home page 
                        */    
                    }else{
                        /*level == 2 or level ==3
                        * Send data to BluePay 
                        */
                        $announce_id = $this->db->select('id')->from('animus_annonce')->order_by('id','DESC')->get()->row()->id;
                        redirect("https://www.bluepaid.com/in.php?&id_boutique=15555www&id_client=105230&montant=".$payamount."&devise=EUR&langue=FR&email_client=".$user_email."&divers=animus|".$announce_id.'|'.$level);
                    }
                } else {
                    $this->display_view('home/annonce/ajouter_annonce');
                }
            } else {
                $this->display_view('home/annonce/ajouter_annonce');
            }
        } else  $this->display_view('home/annonce/error_annonce');

        }
        function update_annonce(){
            is_user();
            if($this->input->post('upgrade')){
                $level = $this->input->post('level');        
                if($level>1){
                    $announce_id = $this->input->post('announce_id');        
                    $payamount = $this->db->get_where('animus_anounce_level',array('id'=>$level))->row()->prix;
                    $user_id = $this->session->userdata('user_id');
                    $user_email= $this->db->get_where('lehu_users',array('id'=>$user_id))->row()->email;
                    redirect("https://www.bluepaid.com/in.php?&id_boutique=15555www&id_client=105230&montant=".$payamount."&devise=EUR&langue=FR&email_client=".$user_email."&divers=animus|".$announce_id.'|'.$level);     
                } else{
                    $this->_data['information']='Ce sont des nouvelles gratuitement.<br/>S\'il vous plaît mettre à niveau vers Premium ou Annonce Annonce A la une';
                    $this->display_view('home/annonce/upgrade_unnonce');
                }

            } else if($this->input->post('free')){
                redirect('annonce/list_annonce');      
            } else{
                $this->display_view('home/annonce/upgrade_unnonce');
            }
            
        }
        function search_annonce($region_id) {
            $per_page = $this->uri->segment(4);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(5);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/annonce/search_annonce/' . '/' . $region_id . '/' . $per_page . '/';
            $config['per_page'] = '5';
            $config['uri_segment'] = 5;
            $config['total_rows'] = $this->Annonce_m->count_search_annonce($region_id);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['search_annonce'] = $this->Annonce_m->search_annonce($region_id, $config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Annonce_m->count_search_annonce($region_id);
            $this->_data['page_title'] = "Liste de annonce";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('home/annonce/search_annonce');
        }

        function detail_annonce($id) {
            $query = $this->Annonce_m->get_annonce_detail($id);
            $this->_data['detail_annonce'] = $query;
            $this->display_view('home/annonce/detail_annonce');
        }

        function search_annonce_adv() {
            $title = $this->input->post('most') != '' ? $this->input->post('most') : '_';
            $region_id = $this->input->post('region') != '' ? $this->input->post('region') : '_';

            $per_page = $this->uri->segment(5);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(6);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['per_page'] = '5';
            $config['uri_segment'] = 6;
            $config['total_rows'] = $this->Annonce_m->count_search_annonce_adv($title, $region_id);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['selected_region'] = $region_id;
            $this->_data['set_most'] = $title;
            $this->_data['search_annonce'] = $this->Annonce_m->search_annonce_adv($title, $region_id, $config['per_page'], $off_set);
            $config['base_url'] = site_url('annonce/pagination_search') . '/' . $title . '/' . $region_id . '/' . $per_page . '/';
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Annonce_m->count_search_annonce_adv($title, $region_id);
            $this->_data['page_title'] = "Liste de annonce";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;

            $this->display_view('home/annonce/search_annonce');
        }

        function pagination_search($title, $region, $per_page) {
            $this->_data['selected_region'] = $region;
            $this->_data['set_most'] = $title;
            $off_set = $this->uri->segment(6);
            if ($off_set == '')
                $offset = 0;
            $config['per_page'] = $per_page;
            $config['uri_segment'] = 6;
            $config['total_rows'] = $this->Annonce_m->count_search_annonce_adv($title, $region);
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['search_annonce'] = $this->Annonce_m->search_annonce_adv($title, $region, $per_page, $off_set);
            $config['base_url'] = site_url('annonce/pagination_search') . '/' . $title . '/' . $region . '/' . $per_page . '/';
            $this->_data['total'] = $this->Annonce_m->count_search_annonce_adv($title, $region);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();

            $this->display_view('home/annonce/search_annonce');
        }
        function list_annonce(){
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 5;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/annonce/list_annonce/' . $per_page . '/';
            $config['per_page'] = '5';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Annonce_m->count_annonce();

            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }

            $this->_data['annonce'] = $this->Annonce_m->get_annonce($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Annonce_m->count_annonce();
            $this->_data['page_title'] = "Liste de annonce";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('home/annonce/list_annonce');

        }

    }
?>